package p2pcdnsim.network.example;

import p2pcdnsim.cdn.ObjectContent;
import p2pcdnsim.network.ApplicationProcess;
import p2pcdnsim.network.DatagramPacket;
import p2pcdnsim.network.DatagramSocket;
import p2pcdnsim.network.GenericAddress;
import desmoj.core.simulator.Model;
import desmoj.core.simulator.SimTime;

public class ExampleUDPClientProcess extends ApplicationProcess {

	public ExampleUDPClientProcess(Model owner, String name) {
		super(owner, name);
	}

	@Override
	protected void run() {
		if(this.isScheduled()) throw new RuntimeException("trying to hold but already scheduled at "+this.scheduledAt());
		hold(new SimTime(1));
		DatagramSocket socket = this.createDatagramSocket();
		
		System.out.println("["+this.currentTime()+"] Client: sending a packet");
		DatagramPacket packet = new DatagramPacket(socket.getAddress(),socket.getPort(),
					                                   new GenericAddress("AddressNode4"),80);
			
		packet.setMessage(new ObjectContent(0,1000));
			
		socket.send(packet);
			
	}
	
}
